package com.xxx.ticket;

/**
 * @version v1.0
 * @Description
 * @Author admin
 * @date 2021/7/30  10:40
 * @Copyright
 */
public class ticket02 extends Thread {
    public static int nnum = 100;
    public static boolean f = true;
    public Object obj = new Object();

    //字节码文件(.calss)对象(class类对象)
    private static synchronized void ifthickta() {
        if (nnum > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ": " + nnum--);
        } else {
            f = false;
        }
    }

    @Override
    public void run() {
        while (f) {
//            synchronized (obj){
            //ifthick();
            ifthickta();
//            }
        }
    }

    //同步方法，当前对象this
    private synchronized void ifthick() {
        if (nnum > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + ": " + nnum--);
        } else {
            f = false;
        }
    }
}